#include "head.h"

int main(int argc,char*argv[])
{

    // 初始化进程池
    proc_status_t list[3];
    initPool(list, 3);

    // 监听socket
    int socket_fd;
    initSocket(&socket_fd, "192.168.106.129", "8080");

    int epoll_fd = epoll_create(1);
    addEpoll(epoll_fd, socket_fd);

    for(int i=0; i<3; i++){
        addEpoll(epoll_fd, list[i].local_socket_fd);
    }
    while(1){

        struct epoll_event events[5];
        int epoll_num = epoll_wait(epoll_fd, events, 5, -1);

        for(int i=0; i<epoll_num; i++){
            struct epoll_event ev = events[i];

            if(ev.data.fd == socket_fd){
                // 有新链接到来
                int net_fd = accept(socket_fd, NULL, NULL);

                // 把net_fd交给子进程
                toSon(net_fd, list, 3);
            }else{
                // 走到这, 说明子进程在给我发信息
                // -> 事情干完, 状态由忙变闲
                
                char buf[60] = {0};
                recv(ev.data.fd, buf, sizeof(buf), 0);

                for(int k=0; k<3; k++ ){
                    if(list[k].local_socket_fd == ev.data.fd){
                        list[k].status = FREE;
                        break;
                    }
                }
            }
        }
    }
    
    close(socket_fd);
    return 0;
}

